<?php
/*
 * @Author: your name
 * @Date: 2021-10-14 13:35:59
 * @LastEditTime: 2021-10-25 14:26:33
 * @LastEditors: 乔
 * @Description: In User Settings Edit
 * @FilePath: \tp30\app\admin\controller\Brand.php
 */

namespace app\admin\controller;

use app\BaseController;
use app\common\traits\Jump;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Brand extends BaseController
{
    use Jump;
    public function brandList()
    {
        $keywords = input('get.keywords');
        $get = Request::get();
        $where = [];
        $whereOr = [];
        if (isset($keywords) && !empty($keywords)) $where['keywords']                      = $keywords;            //['brand_name', 'like', "%{$keywords}%"];
        if (isset($get['recommended']) && $get['recommended'] != "") $where['recommended'] = $get['recommended'];  //['recommended', '=', $get['recommended']];
        if (isset($get['if_show']) && count($get['if_show']) == 1) $where['if_show']       = $get['if_show'];      //['if_show', 'in', $get['if_show']];
        if (isset($get['logoarr']) && count($get['logoarr']) == 1) {
            //$null=$get['logoarr'][0]=='1'?"not null":"null"; $where[] = ['brand_logo',$null,''];
            // if ($get['logoarr'][0] == 1) {
            //     $where[] = ['brand_logo', '<>', ""];
            // } else {
            //     $whereOr[] = ['brand_logo', '=', null];
            //     $whereOr[] = ['brand_logo', '=', ''];
            // }
            if ($get['logoarr'][0] == 1) {
                $where['logoarr'] = "";
            } else {
                $whereOr[] = ['brand_logo', '=', null];
                $whereOr[] = ['brand_logo', '=', ''];
            }
        }
        $list = \app\admin\model\Brand::getBrandList($where, $get, $whereOr);
        // $list=Db::name('brand')
        //     ->where($where)
        //     ->where(function ($q) use($whereOr){
        //         $q->whereOr($whereOr);
        //     })
        //     ->order("sort_order")
        //     ->paginate([
        //         'list_rows'=> 10,
        //         'var_page' => 'page',
        //         'query'=>$get,
        //     ]);
        View::assign('list', $list);
        View::assign($get);
        return view();
    }
    public function brandAdd()
    {
        if (Request::isPost()) {
            $error = $this->uploadImg();
            $post = Request::post();
            if ($error[0]) {
                $post['brand_logo'] = $error[1];
            } else {
                $this->error($error[1]);
            }
            if (!input('?get.id')) {
                list($res, $msg) = \app\admin\model\Brand::addBrand($post);
            } else {
                $post['brand_id'] = input('get.id');
                list($res, $msg) = \app\admin\model\Brand::editBrand($post);
            }
            if ($res) {
                $this->success($msg, url('admin/Brand/brandList'));
            } else {
                $this->error($msg);
            }
        };
        if (input('?get.id')) {
            $list = \app\admin\model\Brand::find(input('get.id'));
            view::assign('list', $list);
        }
        return view();
    }
    public function uploadImg()
    {
        // 获取表单上传文件
        try {
            $files = input('file.img');
        } catch (\Exception $e) {
            return [false, '没上传图'];
        }
        if (!$files) return [false, '没上传图'];
        //判断单图多图
        if (is_array($files)) {
            try {
                validate(['image' => 'fileSize:1048576|fileExt:jpg,png'])
                    ->check(['image' => $files]);
            } catch (ValidateException $e) {
                return [false, $e->getMessage()];
            }
            $path = [];
            foreach ($files as $file) {
                $img = \think\facade\Filesystem::putFile('img', $file);
                $path[] = str_replace('\\', '/', $img);
            }
            $path = implode(',', $path);
        } else {
            try {
                validate(['image' => 'fileSize:1048576|fileExt:jpg,png'])
                    ->check(['image' => $files]);
            } catch (ValidateException $e) {
                return [false, $e->getMessage()];
            }
            $img = \think\facade\Filesystem::putFile('img', $files);
            $path = str_replace('\\', '/', $img);
        }
        return [true, $path];
        exit;
    }
}
